<template>
  <div class="auditCmpt">
    <!-- {{carrier}} -->
    <!-- 查询表单 -->
    <div class="form-baseinfo">
      <LPSearchBar
        v-model="searchModel"
        :search-data="searchData"
        label-width="100px"
        @handleSearchSubmit="handleSearchSubmit"
        @handleSearchReset="handleSearchReset"
      />
    </div>
    <!-- 表格列表 -->
    <div class="form-baseinfo">
      <div class="btn_area">
        <el-button
          v-hasPermi="['LaborProtection.OrdersReturn.Create']"
          size="medium"
          type="primary"
          @click="insertData"
          >新增</el-button
        >
        <el-button size="medium" type="primary" @click="exportInfo">导出数据</el-button>
      </div>
      <LPTablePage
        :show-index="false"
        :show-indexfixed="false"
        index-label="顺序"
        :table-data="tableData"
        :table-column="tableColumn"
        :total="total"
        :current-page="currentPage"
        align="center"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
        <template slot="ordersReturnProducts" slot-scope="{ row }" />

        <template slot="cz" slot-scope="{ row }">
          <el-button type="text" @click="handColumnClick(row, 'detail')"> 详情 </el-button>
          <el-button
            v-if="row.status === 0"
            v-hasPermi="['LaborProtection.OrdersReturn.Update']"
            type="text"
            @click="handColumnClick(row, 'change')"
          >
            编辑
          </el-button>
          <!-- v-hasPermi="['Logistics.Delivery.Update']" -->
          <el-button v-if="row.status == 1 || row.status == 2" type="text" @click="handColumnClick(row, 'cancle')">
            取消
          </el-button>
          <el-button
            v-if="row.status == 2"
            v-hasPermi="['LaborProtection.OrdersReturn.Confirm']"
            type="text"
            @click="handColumnClick(row, 'returnOrder')"
          >
            退货
          </el-button>
          <el-button
            v-if="row.status == -1"
            v-hasPermi="['LaborProtection.OrdersReturn.Delete']"
            type="text"
            @click="handColumnClick(row, 'delete')"
          >
            删除
          </el-button>
        </template>
      </LPTablePage>
    </div>
  </div>
</template>

<script>
import { deleteByIdReturnOrder, cancelByIdReturnOrder, getReturnOrderList } from '@/api/LaborProtection/returnOrder'
import { GetOrdersReturnListExcel } from '@/api/LaborProtection/export'
import { download,parseTime } from '@/utils/lpUtils'
import LPTablePage from '@/components/LPTablePage/index.vue'
import LPSearchBar from '@/components/LPSearchBar/index.vue'

export default {
  components: { LPTablePage, LPSearchBar },
  data() {
    return {
      searchModel: {},
      searchData: [
        { label: '', placeholder: '员工卡号', prop: 'UserCardNo', width: '80px' },
        { label: '', placeholder: '员工姓名', prop: 'UserName', width: '80px' },
        { label: '', placeholder: '商品名称', prop: 'ProductTitle', width: '80px' },
        { label: '', placeholder: '退货单', prop: 'ORNo', width: '80px' },
        {
          label: '',
          placeholder: '状态',
          prop: 'Status',
          type: 'select',
          width: '80px',
          option: [
            { label: '待审核', value: 1 },
            { label: '审核通过', value: 2 },
            { label: '已退货', value: 3 },
            { label: '已作废', value: -1 }
          ]
        },
        {
          label: '',
          prop: 'StartOrderTime',
          type: 'datetimerange',
          placeholder: '开始时间',
          class: 'BeginTime',
          width: '80px'
        }
      ],
      // 列表数据
      tableData: [],
      // 表格字段
      tableColumn: [
        { label: '退货单号', prop: 'orNo' },
        { label: '姓名', prop: 'userName' },
        { label: '工号', prop: 'userJobNum' },
        { label: '部门', prop: 'departmentName' },
        { label: '岗位', prop: 'jobName' },
        { label: '卡号', prop: 'userCardNo' },
        { label: '退回商品', prop: 'productData' },
        { label: '退回金额', prop: 'orMoney' },
        { label: '实际退回金额', prop: 'settlementMoney', width: 110 },
        { label: '订单状态', prop: 'statusName' },
        { label: '创建时间', prop: 'createTime' },
        { label: '操作人', prop: 'modifyName' },
        { label: '操作', prop: 'cz', width: 250, render: true }
      ],
      total: 0,
      currentPage: 1,
      pageSize: 10
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    insertData() {
      this.$router.push({
        path: '/laborProtection/order/returnOrderManage/insertReOrder'
      })
    },
    // 查询按钮
    handleSearchSubmit() {
      this.currentPage = 1
      this.getList()
    },
    // 重置按钮
    handleSearchReset() {
      this.handleSearchSubmit()
    },
    handleSizeChange(v) {
      this.pageSize = v
      this.currentPage = 1
      this.getList()
    },
    handleCurrentChange(v) {
      this.currentPage = v
      this.getList()
    },
    // 表格列按钮点击事件
    handColumnClick(row, name) {
      if (name == 'cancle') {
        this.$confirm('是否取消该条退货记录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            cancelByIdReturnOrder(row.id).then((res) => {
              if (!res.error) {
                this.$message.success('取消成功')
                this.getList()
              }
            })
          })
          .catch(() => {
            this.$message.info('已取消该操作')
          })
      } else if (name == 'returnOrder') {
        this.$router.push({
          path: '/laborProtection/order/returnOrderManage/detail',
          query: {
            data: JSON.stringify({
              id: row.id,
              isAudit: true
            })
          }
        })
      } else if (name == 'detail') {
        this.$router.push({
          path: '/laborProtection/order/returnOrderManage/detail',
          query: {
            data: JSON.stringify({
              id: row.id,
              isAudit: false
            })
          }
        })
      } else if (name == 'delete') {
        this.$confirm('是否删除该条退货记录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            deleteByIdReturnOrder(row.id).then((res) => {
              if (!res.error) {
                this.$message.success('删除成功')
                this.getList()
              }
            })
          })
          .catch(() => {
            this.$message.info('已取消该操作')
          })
      } else if (name == 'change') {
        this.$router.push({
          path: '/laborProtection/order/returnOrderManage/updateReOrder',
          query: {
            id: row.id
          }
        })
      }
    },
    exportInfo() {
      const params = {
        ...this.searchModel,
        SkipCount: this.currentPage,
        MaxResultCount: this.pageSize
      }
      if (typeof this.searchModel.StartOrderTime !== 'undefined') {
        params.SDateTime = parseTime(this.searchModel.StartOrderTime[0])
        params.EDateTime = parseTime(this.searchModel.StartOrderTime[1])
        this.$delete(params, 'StartOrderTime')
      }
      GetOrdersReturnListExcel(params).then((res) => {
        download(res, '退货订单列表')
      })
    },
    getList() {
      const params = {
        ...this.searchModel,
        SkipCount: this.currentPage,
        MaxResultCount: this.pageSize
      }
      if (typeof this.searchModel.StartOrderTime !== 'undefined') {
        params.SDateTime = parseTime(this.searchModel.StartOrderTime[0])
        params.EDateTime = parseTime(this.searchModel.StartOrderTime[1])
        this.$delete(params, 'StartOrderTime')
      }
      getReturnOrderList(params).then((res) => {
        res.items.forEach((element) => {
          element.productData = ''
          element.ordersReturnProducts.forEach((item, index) => {
            if (index > 0) {
              element.productData = element.productData + '、' + item.pTitle
            } else if (index == 0) {
              element.productData = item.pTitle
            }
          })
        })
        this.tableData = res.items
        this.total = res.totalCount
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.auditCmpt {
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
  .form-baseinfo:first-child {
    margin: 20px, 0, 20px, -10px;
    border-radius: 3px;
  }
  .form-baseinfo:last-child {
    background-color: #fff;
    .btn_area {
      padding: 0.2rem;
      border-top-left-radius: 3px;
      border-top-right-radius: 3px;
    }
  }
}

.form-baseinfo {
  // padding: 10px;
}

::v-deep .search-bar input.el-input__inner {
  width: 2rem;
}
</style>
